package com.topisv.tms.web.work;

import java.io.PrintWriter;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.poi.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.topisv.tms.entity.work.WorkOrder;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.service.work.WorkOrderService;

/**
 * 发货任务_control
 * @author ch
 */
@Controller
@RequestMapping({ "/sendTask" })
public class SendTaskController {

	private static final Logger logger = LoggerFactory.getLogger(SendTaskController.class);

	@Autowired
	private WorkOrderService workOrderService;
	
	
	/**
	 * 导向发货不称重任务页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "v2_toSendTaskPage")
	public String toSendTaskPage(ServletRequest request,HttpServletResponse response,Model model){
		return "work/send/send";
	}
	
	/**
	 * 发货称重任务导向页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "v2_toSendWeighTaskPage")
	public String toSendWeightTaskPage(ServletRequest request,HttpServletResponse response,Model model){
		return "work/send/sendweigh";
	}
	
	/**
	 * 发货
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "ajaxSend")
	public void ajaxSend(ServletRequest request,HttpServletResponse response) {
		PrintWriter out=null;
		JSONObject json=null;
		try {
			out = response.getWriter();
			json = new JSONObject();
			String expressNo=request.getParameter("expressNo");
			String weightStr=request.getParameter("weight");
			Integer weight;
			if(weightStr==null){
				weight=0;
			}else{
				weight=Integer.valueOf(weightStr);
			}
			ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			
			String msg=this.workOrderService.send(expressNo,curUser.jobuuid,weight);
			WorkOrder workOrder=this.workOrderService.loadByExpressNo(expressNo);
			json.put("workOrder", workOrder);
			json.put("msg", msg);
			if("success".equals(msg)){
				workOrderService.afterOrderSendOut(workOrder);
			}
			out.print(json);
			
			} catch (Exception e) {
				
				logger.error("error",e);
				json.put("msg", "error");
				out.print(json);
			}	
	}
	
	/**
	 * 发货_不称重发货
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "ajaxSend_noweight")
	public void ajaxSend_noweight(ServletRequest request,HttpServletResponse response) {
		PrintWriter out=null;
		JSONObject json=null;
		try {
			out = response.getWriter();
			json = new JSONObject();
			String expressNo=request.getParameter("expressNo");
			String id=request.getParameter("id");
			if(id.equals("2")){
				//订单号传进来，需先转换成运单号再进行操作
				if(null != workOrderService.loadByOrdernoOrExpressno(expressNo)){
					if(null != workOrderService.loadByOrdernoOrExpressno(expressNo).getExpressNo()){
						expressNo = workOrderService.loadByOrdernoOrExpressno(expressNo).getExpressNo();
					}else{
						expressNo = workOrderService.loadByOrdernoOrExpressno(expressNo).getEbillNo();
					}
				}else{
					expressNo = null ;
				}
				
			}
			ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			
			String msg=this.workOrderService.send_noweight(expressNo,curUser.jobuuid);
			WorkOrder workOrder=this.workOrderService.loadByExpressNo(expressNo);
			json.put("workOrder", workOrder);
			json.put("msg", msg);
			if("success".equals(msg)){
				workOrderService.afterOrderSendOut(workOrder);
			}
			out.print(json);
			
			} catch (Exception e) {
				
				logger.error("error",e);
				json.put("msg", "error");
				out.print(json);
			}	
	}
	
	/**
	 * 发货_称重发货
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "ajaxSend_weight")
	public void ajaxSend_weight(ServletRequest request,HttpServletResponse response) {
		PrintWriter out=null;
		JSONObject json=null;
		try {
			out = response.getWriter();
			json = new JSONObject();
			String expressNo=request.getParameter("expressNo");
			String weight=request.getParameter("weight");
			String id = request.getParameter("id") ;
			if(id.equals("2")){
				//订单号传进来，需先转换成运单号再进行操作
				if(null != workOrderService.loadByOrdernoOrExpressno(expressNo)){
					if(null != workOrderService.loadByOrdernoOrExpressno(expressNo).getExpressNo()){
						expressNo = workOrderService.loadByOrdernoOrExpressno(expressNo).getExpressNo();
					}else{
						expressNo = workOrderService.loadByOrdernoOrExpressno(expressNo).getEbillNo() ;
					}
				}else{
					expressNo = null ;
				}
				
			}
			ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			
			String msg=this.workOrderService.send(expressNo,curUser.jobuuid,Integer.valueOf(weight));
			WorkOrder workOrder=this.workOrderService.loadByExpressNo(expressNo);
			json.put("workOrder", workOrder);
			json.put("msg", msg);
			if("success".equals(msg)){
				workOrderService.afterOrderSendOut(workOrder);
			}
			out.print(json);
			
			} catch (Exception e) {
				
				logger.error("error",e);
				json.put("msg", "error");
				out.print(json);
			}	
	}
}